<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="外协日期" prop="dAssist">
        <el-input
          v-model="queryParams.dAssist"
          placeholder="请输入外协日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="vProjectName">
        <el-input
          v-model="queryParams.vProjectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零件名称" prop="vSpaName">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零件号" prop="vDrawingNo">
        <el-input
          v-model="queryParams.vDrawingNo"
          placeholder="请输入零件号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设计者" prop="vDesigner">
        <el-input
          v-model="queryParams.vDesigner"
          placeholder="请输入设计者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审图人" prop="vReviewer">
        <el-input
          v-model="queryParams.vReviewer"
          placeholder="请输入审图人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂家" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作业号" prop="vWorkNo">
        <el-input
          v-model="queryParams.vWorkNo"
          placeholder="请输入作业号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同编号" prop="vContractNo">
        <el-input
          v-model="queryParams.vContractNo"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleImport"
            >导入</el-button
          >
        </el-col>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="投产日期" align="center" prop="dAssist" />
      <el-table-column label="项目名称" align="center" prop="vProjectName" />
      <el-table-column label="项目编号" align="center" prop="vProjectNo" />
      <el-table-column label="零件名称" align="center" prop="vSpaName" />
      <el-table-column label="零件号" align="center" prop="vDrawingNo" />
      <el-table-column label="图纸数量" align="center" prop="nQty" />
      <el-table-column label="材料" align="center" prop="vMaterial" />
      <el-table-column label="重量g" align="center" prop="nWeight" />
      <el-table-column label="设计者" align="center" prop="vDesigner" />
      <el-table-column label="要求进度" align="center" prop="dExpected" />
      <el-table-column label="要求工期（天）" align="center" prop="nDays" />
      <el-table-column label="厂家" align="center" prop="supplier" />
      <el-table-column label="作业号" align="center" prop="vWorkNo" />
      <el-table-column
        label="成本核算单价未税（元）"
        align="center"
        prop="nCostPrice"
      />

      <el-table-column
        label="合同单价未税（元）"
        align="center"
        prop="nContractPrice"
      />
      <el-table-column
        label="合同未税总价（元）"
        align="center"
        prop="nTotalPrice"
      />
      <el-table-column label="机加合同编号" align="center" prop="vContractNo" />
      <el-table-column
        label="表面处理类别"
        align="center"
        prop="vSurfaceTreat"
      />
      <el-table-column
        label="表面处理单件费用（未税/元）"
        align="center"
        prop="nSurfaceTreatPirce"
      />
      <el-table-column
        label="表面处理费用合计"
        align="center"
        prop="nSurfaceTreatTotal"
      />
      <el-table-column label="喷涂结算数量" align="center" prop="nSprayQty" />
      <el-table-column label="装配间工时" align="center" prop="nWorkHours" />
      <el-table-column
        label="工时单价（元）"
        align="center"
        prop="nWorkPirce"
      />
      <el-table-column
        label="加工费用合计（元）"
        align="center"
        prop="nWorkTotal"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投产登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="外协日期" prop="dAssist">
          <el-input v-model="form.dAssist" placeholder="请输入外协日期" />
        </el-form-item>
        <el-form-item label="项目名称" prop="vProjectName">
          <el-input v-model="form.vProjectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目编号" prop="vProjectNo">
          <el-input v-model="form.vProjectNo" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="零件名称" prop="vSpaName">
          <el-input v-model="form.vSpaName" placeholder="请输入零件名称" />
        </el-form-item>
        <el-form-item label="零件号" prop="vDrawingNo">
          <el-input v-model="form.vDrawingNo" placeholder="请输入零件号" />
        </el-form-item>
        <el-form-item label="数量" prop="nQty">
          <el-input v-model="form.nQty" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="材料" prop="vMaterial">
          <el-input v-model="form.vMaterial" placeholder="请输入材料" />
        </el-form-item>
        <el-form-item label="重量g" prop="nWeight">
          <el-input v-model="form.nWeight" placeholder="请输入重量g" />
        </el-form-item>
        <el-form-item label="设计者" prop="vDesigner">
          <el-input v-model="form.vDesigner" placeholder="请输入设计者" />
        </el-form-item>
        <el-form-item label="要求进度" prop="dExpected">
          <el-input v-model="form.dExpected" placeholder="请输入要求进度" />
        </el-form-item>
        <el-form-item label="实际进度" prop="vActualProgress">
          <el-input
            v-model="form.vActualProgress"
            placeholder="请输入实际进度"
          />
        </el-form-item>
        <el-form-item label="审图人" prop="vReviewer">
          <el-input v-model="form.vReviewer" placeholder="请输入审图人" />
        </el-form-item>
        <el-form-item label="厂家" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入厂家" />
        </el-form-item>
        <el-form-item label="作业号" prop="vWorkNo">
          <el-input v-model="form.vWorkNo" placeholder="请输入作业号" />
        </el-form-item>
        <el-form-item label="成本核算单价不含税" prop="nCostPrice">
          <el-input
            v-model="form.nCostPrice"
            placeholder="请输入成本核算单价不含税"
          />
        </el-form-item>
        <el-form-item label="外委厂家单价含税" prop="nOutsourcingPrice">
          <el-input
            v-model="form.nOutsourcingPrice"
            placeholder="请输入外委厂家单价含税"
          />
        </el-form-item>
        <el-form-item label="合同额单价含税" prop="nContractPrice">
          <el-input
            v-model="form.nContractPrice"
            placeholder="请输入合同额单价含税"
          />
        </el-form-item>
        <el-form-item label="总价含税" prop="nTotalPrice">
          <el-input v-model="form.nTotalPrice" placeholder="请输入总价含税" />
        </el-form-item>
        <el-form-item label="合同编号" prop="vContractNo">
          <el-input v-model="form.vContractNo" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="表面处理" prop="vSurfaceTreat">
          <el-input v-model="form.vSurfaceTreat" placeholder="请输入表面处理" />
        </el-form-item>
        <el-form-item label="单价/表面处理费用" prop="nSurfaceTreatPirce">
          <el-input
            v-model="form.nSurfaceTreatPirce"
            placeholder="请输入单价/表面处理费用"
          />
        </el-form-item>
        <el-form-item label="总价/表面处理费用" prop="nSurfaceTreatTotal">
          <el-input
            v-model="form.nSurfaceTreatTotal"
            placeholder="请输入总价/表面处理费用"
          />
        </el-form-item>
        <el-form-item label="喷涂数量" prop="nSprayQty">
          <el-input v-model="form.nSprayQty" placeholder="请输入喷涂数量" />
        </el-form-item>
        <el-form-item label="编号位置" prop="vNoPosition">
          <el-input v-model="form.vNoPosition" placeholder="请输入编号位置" />
        </el-form-item>
        <el-form-item label="成品/半成品" prop="vFinishedProduct">
          <el-input
            v-model="form.vFinishedProduct"
            placeholder="请输入成品/半成品"
          />
        </el-form-item>
        <el-form-item label="装配间工时" prop="nWorkHours">
          <el-input v-model="form.nWorkHours" placeholder="请输入装配间工时" />
        </el-form-item>
        <el-form-item label="单价" prop="nWorkPirce">
          <el-input v-model="form.nWorkPirce" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="总计" prop="nWorkTotal">
          <el-input v-model="form.nWorkTotal" placeholder="请输入总计" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size: 12px" @click="importTemplate"
            >下载模板</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItem,
  getItem,
  delItem,
  addItem,
  updateItem,
} from "@/api/buy/buy0006";

import { getToken } from "@/utils/auth";

export default {
  name: "Item",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投产登记表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dAssist: null,
        vProjectName: null,
        vProjectNo: null,
        vSpaName: null,
        vDrawingNo: null,
        vDesigner: null,
        vReviewer: null,
        supplier: null,
        vWorkNo: null,
        vContractNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/buy/buy0006/importData",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询投产登记列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iD: null,
        dAssist: null,
        vProjectName: null,
        vProjectNo: null,
        vSpaName: null,
        vDrawingNo: null,
        nQty: null,
        vMaterial: null,
        nWeight: null,
        vDesigner: null,
        dExpected: null,
        vActualProgress: null,
        vReviewer: null,
        supplier: null,
        vWorkNo: null,
        nCostPrice: null,
        nOutsourcingPrice: null,
        nContractPrice: null,
        nTotalPrice: null,
        vContractNo: null,
        vSurfaceTreat: null,
        nSurfaceTreatPirce: null,
        nSurfaceTreatTotal: null,
        nSprayQty: null,
        vNoPosition: null,
        vFinishedProduct: null,
        nWorkHours: null,
        nWorkPirce: null,
        nWorkTotal: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.iD);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投产登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iD = row.iD || this.ids;
      getItem(iD).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改投产登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.iD != null) {
            updateItem(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iDs = row.iD || this.ids;
      this.$modal
        .confirm('是否确认删除投产登记编号为"' + iDs + '"的数据项？')
        .then(function () {
          return delItem(iDs);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/item/export",
        {
          ...this.queryParams,
        },
        `item_${new Date().getTime()}.xlsx`
      );
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "投产登记表导入";
      this.upload.open = true;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
